﻿Public Class ActivHumTyp
    Dim db As New Connexion
    Dim action As String
    Dim Entrer As String
    Dim TextBoxID As String
    Public databaseName As String
    Private Sub ActivHumTyp_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub
    Private Sub ActivHumTyp_LoadTypologie_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ComboBoxCodeCatg.Visible = False
        db.myReq = "SELECT * FROM  [dbo].[ActvCateg]"
        db.Connecter(databaseName)
        Me.ComboBoxCodeCatg.Items.Clear()
        Me.ComboBoxNomCateg.Items.Clear()
        Me.ComboBoxCodeCatg.Items.Add("")
        Me.ComboBoxNomCateg.Items.Add("Tous")
        While db.rdr.Read()
            Me.ComboBoxCodeCatg.Items.Add(db.rdr(0))
            Me.ComboBoxNomCateg.Items.Add(db.rdr(1))
        End While
        db.Disconnecter()
        Me.ComboBoxCodeCatg.SelectedIndex = 0
        Entrer = ""
        DataGridViewTypol.MultiSelect = False
        DataGridViewTypol.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridViewTypol.ReadOnly = True
        miseajour()
    End Sub

    Private Sub miseajour()
        If Me.ComboBoxCodeCatg.Text = "" Then
            db.myReq = "SELECT * FROM  [dbo].[ActivHumTyp] "
        Else
            db.myReq = "SELECT * FROM  [dbo].[ActivHumTyp] where [Id_ActvCateg2]=" & Me.ComboBoxCodeCatg.Text
        End If
        db.Connecter(databaseName)
        DataGridViewTypol.Rows.Clear()
        While db.rdr.Read()
            DataGridViewTypol.Rows.Add(db.rdr(0), db.rdr(1), db.rdr(2), db.rdr(3), db.rdr(4))
        End While
        db.Disconnecter()

        If DataGridViewTypol.RowCount - 1 = 0 Then
            Me.TextBoxCodeTypol.Text = ""
            Me.TextBoxNomTypol.Text = ""
            Me.TextBoxDef.Text = ""
        Else
            DataGridViewTypol.Rows(0).Selected = True
            Me.TextBoxCodeTypol.Text = DataGridViewTypol.SelectedCells(2).Value
            Me.TextBoxNomTypol.Text = DataGridViewTypol.SelectedCells(3).Value
            Me.TextBoxDef.Text = DataGridViewTypol.SelectedCells(4).Value
            TextBoxID = DataGridViewTypol.SelectedCells(0).Value
        End If
    End Sub

    Private Sub ComboBoxNomCatg_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomCateg.TextChanged

        If Me.ComboBoxNomCateg.FindString(Me.ComboBoxNomCateg.Text) > 0 Then
            Dim Position As Int32 = Me.ComboBoxNomCateg.Text.Length
            Me.ComboBoxNomCateg.SelectedIndex = Me.ComboBoxNomCateg.FindString(Me.ComboBoxNomCateg.Text)
            Me.ComboBoxNomCateg.SelectionStart = Position
            Me.ComboBoxNomCateg.SelectionLength = Me.ComboBoxNomCateg.Text.Length - Position
            Me.ComboBoxCodeCatg.SelectedIndex = Me.ComboBoxNomCateg.FindString(Me.ComboBoxNomCateg.Text)
            miseajour()
        Else
            Me.ComboBoxNomCateg.Text = ""
            Me.ComboBoxCodeCatg.Text = ""
            miseajour()
        End If
        miseajour()
    End Sub

    Private Sub ButtonNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouv.Click
        action = "ajouter"
        Me.TextBoxCodeTypol.Text = ""
        Me.TextBoxNomTypol.Text = ""
        Me.TextBoxDef.Text = ""
    End Sub

    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        Entrer = ""
        If action = "ajouter" Then
            db.myReq = "INSERT INTO  [dbo].[ActivHumTyp]([Id_ActvCateg2],[Cod_TypActivHum],[Nom_TypActivHum],[Expl_TypActivHum])VALUES('" & Me.ComboBoxCodeCatg.Text & "','" & Me.TextBoxCodeTypol.Text & "','" & Me.TextBoxNomTypol.Text & "','" & Me.TextBoxDef.Text & "')"
        Else
            db.myReq = "Update  [dbo].[ActvCateg] SET [Cod_EquipCateg2] = '" & Me.TextBoxCodeTypol.Text & "', [Nom_ActvCateg] = '" & Me.TextBoxNomTypol.Text & "',[Exp_ActvCateg] = '" & Me.TextBoxDef.Text & "' WHERE [Id_ActvCateg2]='" & TextBoxID & "'"
        End If
        db.Connecter(databaseName)
        db.Disconnecter()
        miseajour()
    End Sub



End Class